﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Module;
using EMPower.Utility;
using Admin;
using BLL.Common;

public partial class PersonalInfoManage_PersonaStory : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindStroyList();
            try
            {
                string paperId = Request.QueryString["UID"];
                if (!string.IsNullOrWhiteSpace(paperId))
                {
                    PaperInfo paperInfo = BLL.BPaper.getInstance().GetPaperInfo(Convert.ToInt64(paperId));
                    if (paperInfo != null)
                    {
                        this.content.Value = paperInfo.Body;

                        string selectValue = "";
                        foreach (ListItem item in this.StoryDownList.Items)
                        {
                            if (string.Compare(item.Text, paperInfo.Subject) == 0)
                            {
                                this.StoryDownList.SelectedValue = item.Value;
                                selectValue = item.Value;
                                break;
                            }
                        }
                        selectValue = selectValue.Trim();
                        string[] ids = selectValue.Split('-');
                        //long famousRegionId = Convert.ToInt64(ids[0]);
                        long RegionCategoryId = Convert.ToInt64(ids[1]);
                        this.RegionCategoryIdHidden.Value = ids[1];
                        // 绑定列表
                        BindStroyInfoGrid(RegionCategoryId);
                    }
                }
                else
                {
                    BindStroyInfoGrid(0);
                }
            }
            catch (Exception ex)
            {
            }
        }
    }

    private void BindStroyList()
    {
        this.StoryDownList.Items.Clear();
        List<RegionCategoryInfo> regionCategoryList = BLL.BRegionCategory.getInstance().GetRegionCategoryListByType(2);
        ListItem item0 = new ListItem();
        item0.Text = "---请选择---";
        item0.Value = "-1";
        this.StoryDownList.Items.Add(item0);
        if (regionCategoryList != null && regionCategoryList.Count > 0)
        {
            foreach (RegionCategoryInfo regionCategorey in regionCategoryList)
            {
                ListItem item = new ListItem();
                item.Text = regionCategorey.Name;
                string vlue = string.Format("{0}-{1}", Convert.ToString(regionCategorey.FamousRegionId), Convert.ToString(regionCategorey.RegionCategoryId));
                item.Value = vlue;
                this.StoryDownList.Items.Add(item);
            }
        }
    }

    private void BindStroyInfoGrid(long RegionCategoryId)
    {
        // 获取个人事迹
        List<PaperInfo> paperInfoList = new List<PaperInfo>();
        if (RegionCategoryId == 0)
        {
            paperInfoList = BLL.BPaper.getInstance().GetPapersByCategoryType(2);
        }
        else
        {
            paperInfoList = BLL.BPaper.getInstance().GetAllRegionCategoryPage(RegionCategoryId);
        }
        this.StoryInfoDataGrid.DataSource = paperInfoList;
        this.StoryInfoDataGrid.DataBind();
    }

    /// <summary>
    ///  添加个人事迹
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void SubmitButton_Click(object sender, EventArgs e)
    {
        // 选择出来对应的信息
        string selectValue = this.StoryDownList.SelectedValue;
        if ("-1".Equals(selectValue))
        {
            return;
        }

        string title = this.StoryDownList.SelectedItem.Text;
        selectValue = selectValue.Trim();
        string[] ids = selectValue.Split('-');
        long famousRegionId = Convert.ToInt64(ids[0]);
        long RegionCategoryId = Convert.ToInt64(ids[1]);

        PaperInfo paper = new PaperInfo();
        long paperId = IdHelper.GetLongId();
        paper.PaperId = paperId;
        paper.UserPassportId = AuthHelper.CurrentUserPassportId;
        paper.Subject = title;
        paper.Body = this.content.Value;// this.BodyTextBox.Text;
        paper.Type = 1;
        paper.TotalView = 0;
        paper.TotalReplay = 0;
        paper.IsTop = 0;
        paper.isFamousRegion = 0;
        paper.IsAllowComment = 1;
        paper.Status = 1;
        paper.LastUpdate = DateTime.Now;
        paper.DateCreated = DateTime.Now;
        BLL.BPaper.getInstance().Insert(paper);

        FamousRegionPaperInfo famousRegionPaper = new FamousRegionPaperInfo();
        famousRegionPaper.FamousRegionPaperId = IdHelper.GetLongId();
        famousRegionPaper.FamousRegionId = famousRegionId;
        famousRegionPaper.RegionCategoryId = RegionCategoryId;
        famousRegionPaper.PaperId = paperId;
        famousRegionPaper.DateCreated = DateTime.Now;
        BLL.BFamousRegionPaper.getInstance().Insert(famousRegionPaper);
        this.RegionCategoryIdHidden.Value = "";
        Response.Redirect("PersonaStory.aspx");
    }
    //protected void UpdateButton_Click(object sender, EventArgs e)
    //{
    //    string personalStoryIdHidden = this.PersonalStoryIdHidden.Value;
    //    long personalStoryId = Convert.ToInt64(personalStoryIdHidden);
    //    PaperInfo paperInfo = BLL.BPaper.getInstance().GetPaperInfo(personalStoryId);
    //    //paperInfo.Subject = this.SubjectTextBox.Text;
    //    paperInfo.Body = this.content.Value;//this.BodyTextBox.Text;
    //    paperInfo.LastUpdate = DateTime.Now;

    //    BLL.BPaper.getInstance().Update(paperInfo);
    //    this.PersonalStoryIdHidden.Value = "";
    //    Response.Redirect("PersonaStory.aspx");
    //}
    protected void StoryDownList_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.StoryInfoDataGrid.CurrentPageIndex = 0;
        // 选择出来对应的信息
        string selectValue = this.StoryDownList.SelectedValue;
        if ("-1".Equals(selectValue))
        {
            this.RegionCategoryIdHidden.Value = "";
            this.content.Value = "";
            BindStroyInfoGrid(0);
            return;
        }
        string[] ids = selectValue.Split('-');
        long famousRegionId = Convert.ToInt64(ids[0]);
        long RegionCategoryId = Convert.ToInt64(ids[1]);
        this.RegionCategoryIdHidden.Value = ids[1];
        List<FamousRegionPaperInfo> famousRegionPapersList = BLL.BFamousRegionPaper.getInstance().GetFamousRegionPagersByIds(famousRegionId, RegionCategoryId);
        if (famousRegionPapersList != null && famousRegionPapersList.Count > 0)
        {
            FamousRegionPaperInfo famousRegionPaper = famousRegionPapersList[0];
            long paperId = famousRegionPaper.PaperId;
            PaperInfo paperInfo = BLL.BPaper.getInstance().GetPaperInfo(paperId);
            if (paperInfo != null)
            {
                this.content.Value = paperInfo.Body;
                // 绑定列表
                BindStroyInfoGrid(RegionCategoryId);
            }
        }
        else
        {
            this.content.Value = "";
            this.StoryInfoDataGrid.DataSource = new List<PaperInfo>();
            this.StoryInfoDataGrid.DataBind();
        }
    }
    protected void StoryInfoDataGrid_DeleteCommand(object source, DataGridCommandEventArgs e)
    {
        long paperId = Convert.ToInt64(e.Item.Cells[0].Text);
        PaperInfo paperInfo = BLL.BPaper.getInstance().GetPaperInfo(paperId);
        // 0表示删除，2表示拉黑（未完成）
        paperInfo.Status = 0;
        paperInfo.LastUpdate = DateTime.Now;
        BLL.BPaper.getInstance().Update(paperInfo);
        BindStroyInfoGrid(string.IsNullOrWhiteSpace(this.RegionCategoryIdHidden.Value) ? 0 : Convert.ToInt64(this.RegionCategoryIdHidden.Value));
    }
    protected void StoryInfoDataGrid_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
    {
        this.StoryInfoDataGrid.CurrentPageIndex = e.NewPageIndex;
        BindStroyInfoGrid(string.IsNullOrWhiteSpace(this.RegionCategoryIdHidden.Value) ? 0 : Convert.ToInt64(this.RegionCategoryIdHidden.Value));
    }
}